﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Common;
using AutoFacUtils;
using CashFlow.ShopManage.Def;
using CashFlow.ShopManage.Models;
using CashFlow.ShopManage.Filters;
using CashFlow.ShopManage.MyActionResult;
using CashFlow.ShopManage.Validator;
using CashFlow.ShopManage.Service;
using CashFlow.ShopManage.Common;

namespace CashFlow.ShopManage.Controllers
{
    [AllowAnonymous]
    [NotLoginFilter]
    public class AccountController : Controller
    {
        private IAdminService GetAdminService() { return AutoFacHelper.Get<IAdminService>(); }

        // GET: Account
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        [ValidatorFilter(typeof(LoginUserInfoModelValidator))]
        public ActionResult Login(LoginUserInfoModel model)
        {
            bool success = false;
            string msg = "";

            var service = GetAdminService();
            success = service.Login(model.UserName, model.Pwd);
            if (!success)
            {
                msg = "用户名和密码不匹配";
            }

            return MyCommonTool.CreateOperationResult(success, msg, null);
        }

        public ActionResult VerifyCode()
        {
            string code = "";
            byte[] buf = VerifyCodeHelper.GetVerifyCode(out code);
            Session["VerifyCode"] = code;
            FileContentResult tcr = new FileContentResult(buf, "image/jpeg");
            return tcr;
        }
    }
}